

<!DOCTYPE html>
<html lang="zh" xmlns:v-bind="http://www.w3.org/1999/xhtml">

<head><meta name="generator" content="Hexo 3.9.0">
    <title>GO学习项目 - suke`s Notice</title>
<meta charset="utf-8">
<meta name="X-UA-Compatible" content="IE=edge">
<meta name="author" content="suke">
<meta name="description" content="Go-echo-demohttps://github.com/Treblex/go-echo-demo
go 语言...">
<meta name="keywords" content="javascript html css php server npm nodejs">

<link rel="alternate" type="application/atom+xml" title="ATOM 1.0" href="/atom.xml">

    <meta charset="utf-8">
    <meta name="X-UA-Compatible" content="IE=edge">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
    <meta content="telephone=no" name="format-detection">
    <meta name="renderer" content="webkit">
    <meta name="theme-color" content="#ffffff">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/css/bootstrap.min.css" integrity="sha256-eSi1q2PG6J7g7ib17yAaWMcrr5GrtohYChqibrV7PBE=" crossorigin="anonymous">
<link rel="stylesheet" href="/css/journal.css?39155922">

<script src="/js/loadCSS.js"></script>
<script>
    loadCSS("https://fonts.googleapis.com/css?family=Lora|Montserrat|Fira+Mono|Material+Icons");
    (function (d) {
        var config = {
                kitId: 'dwg1tuc',
                scriptTimeout: 3000,
                async: true
            },
            h = d.documentElement, t = setTimeout(function () {
                h.className = h.className.replace(/\bwf-loading\b/g, "") + " wf-inactive";
            }, config.scriptTimeout), tk = d.createElement("script"), f = false,
            s = d.getElementsByTagName("script")[0], a;
        h.className += " wf-loading";
        tk.src = 'https://use.typekit.net/' + config.kitId + '.js';
        tk.async = true;
        tk.onload = tk.onreadystatechange = function () {
            a = this.readyState;
            if (f || a && a != "complete" && a != "loaded") return;
            f = true;
            clearTimeout(t);
            try {
                Typekit.load(config)
            } catch (e) {
            }
        };
        s.parentNode.insertBefore(tk, s)
    })(document);
</script>
<noscript>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lora|Montserrat|Anonymous+Pro:400|Material+Icons">
</noscript>
</head>
<body>
<div id="top"></div>
<div id="app">
<div class="single-column-drawer-container" ref="drawer" v-bind:class="{ 'single-column-drawer-container-active': isDrawerOpen }">
    <div class="drawer-content">
        <div class="drawer-menu">
            <a class="a-block drawer-menu-item false" href="https://sukeai.netlify.com">
                Home
            </a>
            

            
            
            <a class="a-block drawer-menu-item false" href="/firends/index.html">
                朋友👬
            </a>
            
            <a class="a-block drawer-menu-item false" href="/projects/index.html">
                空闲下来做的项目
            </a>
            
            <a class="a-block drawer-menu-item false" href="/about/index.html">
                关于我
            </a>
            

            
            <a class="a-block drawer-menu-item" href="/atom.xml">
                RSS
            </a>
            
        </div>
    </div>
</div>
<transition name="fade">
    <div v-bind:class="{ 'single-column-drawer-mask': mounted }" v-if="isDrawerOpen" v-on:click="toggleDrawer"></div>
</transition>
<nav ref="navBar" class="navbar sticky-top navbar-light single-column-nav-container">
    <div ref="navBackground" class="nav-background"></div>
    <div class="container container-narrow nav-content">
        <button id="nav_dropdown_btn" class="nav-dropdown-toggle" type="button" v-on:click="toggleDrawer">
            <i class="material-icons">
                menu
            </i>
        </button>
        <a ref="navTitle" class="navbar-brand" href="/">
            suke`s Notice
        </a>
    </div>
</nav>
<div class="single-column-header-container" ref="pageHead" v-bind:style="{ transform: 'translateZ(0px) translateY('+.3*scrollY+'px)', opacity: 1-navOpacity }">
    <a href="/">
        <div class="single-column-header-title">suke`s Notice</div>
        <div class="single-column-header-subtitle">hexo博客</div>
    </a>
</div>
<div ref="sideContainer" class="side-container">
    <a class="a-block nav-head false" href="/">
        <div class="nav-title">
            有猫病.
        </div>
        <div class="nav-subtitle">
             她的眼里有光,她爱的人是谁呢 
        </div>
    </a>

    <div class="nav-link-list">
        

        
        
        <a class="a-block nav-link-item false" href="/firends/index.html">
            朋友👬
        </a>
        
        <a class="a-block nav-link-item false" href="/projects/index.html">
            空闲下来做的项目
        </a>
        
        <a class="a-block nav-link-item false" href="/about/index.html">
            关于我
        </a>
        

        
        <a class="a-block no-tint nav-link-item" href="/atom.xml">
            RSS
        </a>
        
    </div>

    
    <div class="nav-footer">
        Proudly published with Hexo<br>
        
        Theme <a href="https://github.com/SumiMakito/hexo-theme-journal/" target="_blank" rel="noreferrer noopener">Journal.</a> by <a href="https://mak1t0.cc/" target="_blank" rel="noreferrer noopener">Makito</a><br>
        
        &copy; 2020 <a href="https://sukeai.netlify.com">suke`s Notice</a>
    </div>
</div>
<div ref="extraContainer" class="extra-container">
    <div class="pagination">
        <a id="globalBackToTop" class="pagination-action animated-visibility" href="#top" :class="{ invisible: scrollY == 0 }">
            <i class="material-icons pagination-action-icon">
                keyboard_arrow_up
            </i>
        </a>

        
    </div>
</div>



<div ref="streamContainer" class="stream-container">
    <div class="post-list-container post-list-container-shadow">
        <div class="post">
            <div class="post-head-wrapper-text-only" style="background-image: url('')">
                <div class="post-title">
                    GO学习项目
                    <div class="post-meta">
                        <time datetime="2020-01-26T14:33:00.000Z" itemprop="datePublished">
                            2020-01-26 14:33
                        </time>&nbsp;
                        
    
                        
                    </div>
                </div>
            </div>
    
            <div class="post-body-wrapper">
                <div class="post-body">
                    <h1 id="Go-echo-demo"><a href="#Go-echo-demo" class="headerlink" title="Go-echo-demo"></a>Go-echo-demo</h1><p><a href="https://github.com/Treblex/go-echo-demo" target="_blank" rel="noopener">https://github.com/Treblex/go-echo-demo</a></p>
<h2 id="go-语言入门"><a href="#go-语言入门" class="headerlink" title="go 语言入门"></a>go 语言入门</h2><p>推荐这个 <a href="https://tour.golang.org/welcome/2" target="_blank" rel="noopener">https://tour.golang.org/welcome/2</a> 还没看完 嘿嘿<br>Go 语言在线练习场，讲解很细</p>
<h2 id="web-框架"><a href="#web-框架" class="headerlink" title="web 框架"></a>web 框架</h2><p>简单看了一些基础我就来搞这个了，这个项目也是一个 web 实践</p>
<blockquote>
<h3 id="echo-官方文档-注意首页有几个很有用的简单实例，但是你按照文档目录是无法返回这一页的-😂-当时找了好久找不到"><a href="#echo-官方文档-注意首页有几个很有用的简单实例，但是你按照文档目录是无法返回这一页的-😂-当时找了好久找不到" class="headerlink" title="echo 官方文档 注意首页有几个很有用的简单实例，但是你按照文档目录是无法返回这一页的 😂 当时找了好久找不到"></a>echo <a href="http://go-echo.org/" target="_blank" rel="noopener">官方文档</a> 注意首页有几个很有用的简单实例，但是你按照文档目录是无法返回这一页的 😂 当时找了好久找不到</h3><p>这个项目就是 据说超快</p>
</blockquote>
<blockquote>
<h3 id="gin-官方文档"><a href="#gin-官方文档" class="headerlink" title="gin 官方文档"></a>gin <a href="https://gin-gonic.com/zh-cn/docs/" target="_blank" rel="noopener">官方文档</a></h3><p>江湖人称：用过都说好，据说开发是可以自动重启 不用手动运行项目 想试试，但是我懒 和上边那个一样快，又一个扩展项目 好像叫 green 可以自动生成文档，不过很久没更新了，有兴趣了解一下这个怎么实现的</p>
</blockquote>
<blockquote>
<h3 id="iris-官方文档"><a href="#iris-官方文档" class="headerlink" title="iris 官方文档"></a>iris <a href="https://iris-go.com/" target="_blank" rel="noopener">官方文档</a></h3><p>大型项目支持</p>
</blockquote>
<h2 id="数据库支持"><a href="#数据库支持" class="headerlink" title="数据库支持"></a>数据库支持</h2><p><a href="https://jasperxu.github.io/gorm-zh/crud.html#q" target="_blank" rel="noopener">gorm 中文文档</a> 我看的这个 因为搜索引擎先找到了这个 后来才发现其实不是官方的 这里&gt;&gt;<br><a href="https://gorm.io/" target="_blank" rel="noopener">gorm 官方文档</a></p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"github.com/jinzhu/gorm"</span> <span class="comment">//数据库操作框架 支持模型和链式操作 不用写sql了</span></span><br><span class="line">_ <span class="string">"github.com/go-sql-driver/mysql"</span> <span class="comment">//go语言sql驱动</span></span><br><span class="line"></span><br><span class="line">db, err := gorm.Open(<span class="string">"mysql"</span>, config.DataBase)</span><br><span class="line"><span class="keyword">if</span> err != <span class="literal">nil</span> &#123;</span><br><span class="line">    <span class="built_in">panic</span>(err)</span><br><span class="line">&#125;</span><br><span class="line">db.LogMode(<span class="literal">true</span>)</span><br><span class="line"><span class="keyword">defer</span> db.Close() <span class="comment">//defer表示函数结束是调用 ，  但是我不想在每个函数都重新打开链接，所以我创建了一个 util包 并暴露了 DB 供全局使用，我在 main.go 的函数结尾出调用了 defer db.Close()，但我并不知道他是否关闭了链接，可以知道的是链接确实只创建了一次，并一直保持</span></span><br></pre></td></tr></table></figure>

<h1 id="📃-关于这个项目"><a href="#📃-关于这个项目" class="headerlink" title="📃 关于这个项目"></a>📃 关于这个项目</h1><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">.</span><br><span class="line">├── Dockerfile <span class="comment">//docker构建 我还在摸索 不过确实运行成功了</span></span><br><span class="line">├── README.md</span><br><span class="line">├── build.linux.sh  <span class="comment">//linux构建文件，我在一段时间后才意识到 编译到不同平台到二进制文件也会有不同，不该是0和1吗 😂</span></span><br><span class="line">├── build.sh         <span class="comment">//mac构建</span></span><br><span class="line">├── build.win.sh    <span class="comment">//win .exe文件</span></span><br><span class="line">├── config</span><br><span class="line">│   └── config.<span class="keyword">go</span>   <span class="comment">//配置文件</span></span><br><span class="line">├── dist            <span class="comment">//编译后到文件</span></span><br><span class="line">│   └── main-linux</span><br><span class="line">├── <span class="keyword">go</span>.mod          <span class="comment">//go模块</span></span><br><span class="line">├── <span class="keyword">go</span>.sum          <span class="comment">//git clone 之后可能需要删除这个文件重新按照mod</span></span><br><span class="line">├── h5              <span class="comment">// 这是一个简单到请求接口到页面，在尝试了一段时间layui写admin页面之后，觉得直接写接口好了，&gt;&gt;&gt; mvc 主要是卡在 模版分离之后 header 和 footer 之类到layout文件 如何请求数据，因为他不对应一个路由，当前找到到方案是模版函数 但是没有运行成功 可能我绑定模版render到方法也有问题, 也尝试了ifarme 不太喜欢，还是后边试一下antd或者自己搞一下vue或者react的后台页面</span></span><br><span class="line">│   ├── api.js</span><br><span class="line">│   ├── index.html</span><br><span class="line">│   ├── main.css</span><br><span class="line">│   └── main.js</span><br><span class="line">├── main.<span class="keyword">go</span>         <span class="comment">//入口文件</span></span><br><span class="line">├── modal           <span class="comment">//模型文件， 数据库查询的方法</span></span><br><span class="line">│   └── user.<span class="keyword">go</span></span><br><span class="line">├── router          <span class="comment">//路由文件</span></span><br><span class="line">│   ├── admin       <span class="comment">//内包含一个类似router.go的入口文件，和其他路由文件</span></span><br><span class="line">│   └── router.<span class="keyword">go</span>   <span class="comment">//暴露一个 Init 方法接收 echo对象, 用于声明当前路面的子文件夹路由对象 或者 路由</span></span><br><span class="line">&gt;&gt;&gt;&gt;&gt;&gt; 例子 &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</span><br><span class="line">    <span class="comment">// 项目首页</span></span><br><span class="line">	admin.Init(e, baseURL)</span><br><span class="line"></span><br><span class="line">	<span class="comment">// 入口</span></span><br><span class="line">	e.GET(baseURL, <span class="function"><span class="keyword">func</span><span class="params">(c echo.Context)</span> <span class="title">error</span></span> &#123;</span><br><span class="line">		<span class="keyword">return</span> c.String(http.StatusOK, <span class="string">"hello world！"</span>)</span><br><span class="line">    &#125;)</span><br><span class="line">&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</span><br><span class="line">├── static  <span class="comment">//资源文件</span></span><br><span class="line">├── template    <span class="comment">//模板文件 弃用mvc之后 只保留了error</span></span><br><span class="line">│   └── error.html</span><br><span class="line">└── util    <span class="comment">//工具包</span></span><br><span class="line">    ├── db.<span class="keyword">go</span>   <span class="comment">// 暴露一个DB对象 用户操作数据库,启动后仅链接一次</span></span><br><span class="line">    ├── <span class="keyword">return</span>.<span class="keyword">go</span>   <span class="comment">//返回的json对象格式 和错误码声明</span></span><br><span class="line">    ├── template.<span class="keyword">go</span> <span class="comment">//声明模版</span></span><br><span class="line">    ├── time.<span class="keyword">go</span> <span class="comment">//时间格式化  以及  链接数据库是自动格式化的一个 结构体</span></span><br><span class="line">    └── util.<span class="keyword">go</span> <span class="comment">//暂无内容--</span></span><br></pre></td></tr></table></figure>

<h2 id="🦊-杂项-遇到的一些小问题"><a href="#🦊-杂项-遇到的一些小问题" class="headerlink" title="🦊 杂项 遇到的一些小问题"></a>🦊 杂项 遇到的一些小问题</h2><h3 id="时间戳格式化"><a href="#时间戳格式化" class="headerlink" title="时间戳格式化"></a>时间戳格式化</h3><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> timeLayout <span class="keyword">string</span> = <span class="string">"2006年01月02日 15:04:05"</span></span><br><span class="line">t := time.Now()</span><br><span class="line">t.Format(timeLayout)</span><br><span class="line"></span><br><span class="line"><span class="comment">//这里 2006 01 02 15 04 05 必须是固定的才能解析到正确的时间</span></span><br><span class="line"><span class="comment">//我在 util/time.go 内实现了一个简单的方法以使用 y-m-d h:i:s 来格式化时间 主要还是因为懒得记</span></span><br><span class="line"><span class="comment">//  Format 也提供了很多的默认layout，默认layou英文支持做的好一些，中文的显示自定义的layout满足需求</span></span><br></pre></td></tr></table></figure>

<h3 id="数据库存取时间类型"><a href="#数据库存取时间类型" class="headerlink" title="数据库存取时间类型"></a>数据库存取时间类型</h3><h4 id="gorm-时间类型自定义解析格式"><a href="#gorm-时间类型自定义解析格式" class="headerlink" title="gorm 时间类型自定义解析格式"></a>gorm 时间类型自定义解析格式</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// LocalTime 继承time.Time类型</span></span><br><span class="line"><span class="keyword">type</span> LocalTime <span class="keyword">struct</span> &#123;</span><br><span class="line">	time.Time</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 自定义的 layout</span></span><br><span class="line"><span class="keyword">var</span> timeLayout <span class="keyword">string</span> = <span class="string">"2006年01月02日 15:04:05"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// MarshalJSON json格式化时间的方法</span></span><br><span class="line"><span class="comment">// 在网上搜到的方式是格式化为时间戳的  不符合我的需求，这个方法是从 time源代码里找到的,直接修改默认 layout为自定义即可</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(t LocalTime)</span> <span class="title">MarshalJSON</span><span class="params">()</span> <span class="params">([]<span class="keyword">byte</span>, error)</span></span> &#123;</span><br><span class="line">	<span class="keyword">if</span> y := t.Year(); y &lt; <span class="number">0</span> || y &gt;= <span class="number">10000</span> &#123;</span><br><span class="line">		<span class="comment">// RFC 3339 is clear that years are 4 digits exactly.</span></span><br><span class="line">		<span class="comment">// See golang.org/issue/4556#c15 for more discussion.</span></span><br><span class="line">		<span class="keyword">return</span> <span class="literal">nil</span>, errors.New(<span class="string">"Time.MarshalJSON: year outside of range [0,9999]"</span>)</span><br><span class="line">	&#125;</span><br><span class="line">	b := <span class="built_in">make</span>([]<span class="keyword">byte</span>, <span class="number">0</span>, <span class="built_in">len</span>(timeLayout)+<span class="number">2</span>)</span><br><span class="line">	b = <span class="built_in">append</span>(b, <span class="string">'"'</span>)</span><br><span class="line">	b = t.AppendFormat(b, timeLayout)</span><br><span class="line">	b = <span class="built_in">append</span>(b, <span class="string">'"'</span>)</span><br><span class="line">	<span class="keyword">return</span> b, <span class="literal">nil</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//下边两个方法是gorm需要,不需要修改，如果没有使用 gorm则不需要</span></span><br><span class="line"><span class="comment">// Value Value</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(t LocalTime)</span> <span class="title">Value</span><span class="params">()</span> <span class="params">(driver.Value, error)</span></span> &#123;</span><br><span class="line">	<span class="keyword">var</span> zeroTime time.Time</span><br><span class="line">	<span class="keyword">if</span> t.Time.UnixNano() == zeroTime.UnixNano() &#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">nil</span>, <span class="literal">nil</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> t.Time, <span class="literal">nil</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Scan Scan</span></span><br><span class="line"><span class="function"><span class="keyword">func</span> <span class="params">(t *LocalTime)</span> <span class="title">Scan</span><span class="params">(v <span class="keyword">interface</span>&#123;&#125;)</span> <span class="title">error</span></span> &#123;</span><br><span class="line">	value, ok := v.(time.Time)</span><br><span class="line">	<span class="keyword">if</span> ok &#123;</span><br><span class="line">		*t = LocalTime&#123;Time: value&#125;</span><br><span class="line">		<span class="keyword">return</span> <span class="literal">nil</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> fmt.Errorf(<span class="string">"can not convert %v to timestamp"</span>, v)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="存取时间的时候相差-8-小时"><a href="#存取时间的时候相差-8-小时" class="headerlink" title="存取时间的时候相差 8 小时"></a>存取时间的时候相差 8 小时</h4><figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//DataBase 数据库配置 username:password@host/database_name?param</span></span><br><span class="line">DataBase <span class="keyword">string</span> = <span class="string">"root:2568597007suke@(localhost:3306)/test?charset=utf8mb4&amp;parseTime=true&amp;loc=Asia%2fShanghai"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//charset=utf8mb4 数据库编码</span></span><br><span class="line"><span class="comment">//parseTime=true    自动解析时间 time.Time 类型解析显示不正确  z100:h1223 之类的一个字符串</span></span><br><span class="line"><span class="comment">//loc=Asia%2fShanghai  默认亚洲时间，数据库存储 detatime 默认为utc时区 也就是会比国内早8个小时</span></span><br></pre></td></tr></table></figure>

<h4 id="查询数据时-隐藏某些隐私字段"><a href="#查询数据时-隐藏某些隐私字段" class="headerlink" title="查询数据时 隐藏某些隐私字段"></a>查询数据时 隐藏某些隐私字段</h4><p>gorm:”-“ 在保存数据的时候会忽略 查询是同样显示</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//暂时的解决方案是声明两个模型，在查询时仅显示必要字段</span></span><br><span class="line"><span class="comment">//由于gorm使用结构名 + s  例如 users articles 默认为约定表名，所以声明另外的模型是需要在查询数据只指定表名</span></span><br><span class="line"><span class="comment">// gorm.Table("users")</span></span><br></pre></td></tr></table></figure>

<h4 id="！gorm-更新-或者-添加删除字段后-查询影响的数据行数一直为-0，因此无法知道是否更新成功-或者-保存数据成功"><a href="#！gorm-更新-或者-添加删除字段后-查询影响的数据行数一直为-0，因此无法知道是否更新成功-或者-保存数据成功" class="headerlink" title="！gorm 更新 或者 添加删除字段后 查询影响的数据行数一直为 0，因此无法知道是否更新成功 或者 保存数据成功"></a>！gorm 更新 或者 添加删除字段后 查询影响的数据行数一直为 0，因此无法知道是否更新成功 或者 保存数据成功</h4><p>在操作之后重新赋值 db 以获取新的位置 而不是直接使用 db.RowsAffected</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">row := db.Model(&amp;User&#123;ID: id&#125;).Updates(data)</span><br><span class="line">row.RowsAffected</span><br></pre></td></tr></table></figure>

<h3 id="go-get-安装完包之后不能使用命令后直接执行"><a href="#go-get-安装完包之后不能使用命令后直接执行" class="headerlink" title="go get 安装完包之后不能使用命令后直接执行"></a>go get 安装完包之后不能使用命令后直接执行</h3><p>path 设置错误</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># //go mod需要开启</span></span><br><span class="line"><span class="built_in">export</span> GO111MODULE=on</span><br><span class="line"><span class="comment"># //指定代理</span></span><br><span class="line"><span class="built_in">export</span> GOPROXY=https://goproxy.cn;</span><br><span class="line"><span class="comment"># goroot  go环境所在目录  系统包</span></span><br><span class="line"><span class="built_in">export</span> GOROOT=/usr/<span class="built_in">local</span>/go</span><br><span class="line"><span class="comment"># 安装的package所在目录  自己定义，修改目录之后需要重新安装package</span></span><br><span class="line"><span class="built_in">export</span> GOPATH=~/gowork</span><br><span class="line"><span class="comment"># 将GOPATH/bin 加入全局path之后 自定义安装的package就可以全局运行了 fresh govender</span></span><br><span class="line"><span class="built_in">export</span> PATH=<span class="variable">$GOPATH</span>/bin:<span class="variable">$GOPATH</span>:<span class="variable">$PATH</span></span><br></pre></td></tr></table></figure>

<h3 id="web-开发时自动重启项目"><a href="#web-开发时自动重启项目" class="headerlink" title="web 开发时自动重启项目"></a>web 开发时自动重启项目</h3><p><a href="https://github.com/gravityblast/fresh" target="_blank" rel="noopener">https://github.com/gravityblast/fresh</a><br>看起来像是检查 ctrl s 的时候重新编译文件并执行，文件没有修改的时候也会触发重启</p>
<h2 id="⚠️-暂未解决的问题"><a href="#⚠️-暂未解决的问题" class="headerlink" title="⚠️ 暂未解决的问题"></a>⚠️ 暂未解决的问题</h2><h2 id="环境设置"><a href="#环境设置" class="headerlink" title="环境设置"></a>环境设置</h2><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="builtin-name">export</span> <span class="attribute">GO111MODULE</span>=on <span class="attribute">GOPROXY</span>=https://goproxy.cn;</span><br><span class="line"><span class="builtin-name">export</span> <span class="attribute">GOROOT</span>=/usr/local/go</span><br><span class="line"><span class="builtin-name">export</span> <span class="attribute">GOPATH</span>=~/gowork</span><br><span class="line"></span><br><span class="line"><span class="builtin-name">export</span> <span class="attribute">PATH</span>=<span class="variable">$GOPATH</span>/bin:$GOPATH:$PATH</span><br></pre></td></tr></table></figure>


                </div>
            </div>
    
            <nav class="post-pagination">
    
    <a class="newer-posts" href="/2020-03-16/Flutter实践.html">
        Previous post<br>Flutter实践
    </a>
    
    <span class="page-number"></span>
    
    <a class="older-posts" href="/2020-01-26/在微信登陆遇到重复循环登陆的问题.html">
        Next post<br>在微信登陆遇到重复循环登陆的问题
    </a>
    
</nav>

    
            


        </div>
    </div>
    <div class="single-column-footer">
    Proudly published with Hexo<br>
    
    Theme <a href="https://github.com/SumiMakito/hexo-theme-journal/" target="_blank" rel="noreferrer noopener">Journal.</a> by <a href="https://mak1t0.cc/" target="_blank" rel="noreferrer noopener">Makito</a><br>
    
    &copy; 2020 <a href="https://sukeai.netlify.com">suke`s Notice</a>
</div>
</div>

</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.3.1/dist/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.4/dist/umd/popper.min.js" integrity="sha256-EGs9T1xMHdvM1geM8jPpoo8EZ1V1VRsmcJz8OByENLA=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.1.3/dist/js/bootstrap.min.js" integrity="sha256-VsEqElsCHSGmnmHXGQzvoWjWwoznFSZc6hs7ARLRacQ=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.min.js" integrity="sha256-FtWfRI+thWlNz2sB3SJbwKx5PgMyKIVgwHCTwa3biXc=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/smooth-scroll@14.2.1/dist/smooth-scroll.polyfills.min.js" integrity="sha256-CI4Gq5E0io1Pv0xM3qPM+NUIOhbIBvC3GiN1Y4KhXpw=" crossorigin="anonymous"></script>
<script src="/js/journal.js?9496634"></script>



</body>
</html>
